<?php

/**
 * @author Thanhansoft
 * @email thanhansoft@gmail.com
 * @website http://grouplaptrinh.com
 * @copyright 5/2/2011 7:11
 */

session_start();
ob_start();
define('IN_SITE', true);
error_reporting(E_ALL);

//include load framework
if ($handle = opendir('library')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            require_once 'library/'.$file;
        }
    }
    closedir($handle);
}

$config = new Config();

//Defines the folder where our project
$path = $config->configweb['path'];

//Removing the path to the start and creates the var $ url with the rest
$url = $_SERVER['REQUEST_URI'];
$url = 	str_replace($path,"",$url);

//creates an array with the rest of the url
$array_tmp_uri = preg_split('[\\/]', $url, -1, PREG_SPLIT_NO_EMPTY);

//Here we define what is the rest of the URL
$array_uri['controller'] 	= empty($array_tmp_uri[0]) ? $config->configweb['model'] : $array_tmp_uri[0]; //a class

empty($array_tmp_uri[1]) ? $array_tmp_uri[1] = '' : $array_tmp_uri[1]; //coi lai
if(in_array($array_tmp_uri[1], array('en'))){
    $array_uri['lang']			= empty($array_tmp_uri[1]) ? '' : $array_tmp_uri[1]; //a seo url
    $array_uri['method']		= empty($array_tmp_uri[2]) ? '' : $array_tmp_uri[2]; //a function
    $array_uri['var'] = '';
    if(!empty($array_tmp_uri[3])){$total = count($array_tmp_uri);$i = 2;while($i < $total){$array_uri['var'] .= $array_tmp_uri[$total - ($total - $i)].'/';$i++;}$array_uri['var'] = substr($array_uri['var'], 0, -1);}
}else{
    $array_uri['method']		= empty($array_tmp_uri[1]) ? '' : $array_tmp_uri[1]; //a function
    $array_uri['var'] = '';
    if(!empty($array_tmp_uri[2])){$total = count($array_tmp_uri);$i = 2; while($i < $total){$array_uri['var'] .= $array_tmp_uri[$total - ($total - $i)].'/';$i++;} $array_uri['var'] = substr($array_uri['var'], 0, -1);}
}

define('PATH', $path);
define('TEMPLATE', $path.'/'.$config->configweb['template_site']);
define('TITLE', $config->configweb['titleweb']);
define('KEYWORD', $config->configweb['keyword']);
define('DESCRIPTION', $config->configweb['description']);
define('LANG', in_array($array_tmp_uri[1], array('en')) ? $array_tmp_uri[1] : '');
define('LANGURL', in_array($array_tmp_uri[1], array('en')) ? '/'.$array_tmp_uri[1] : '');

//load Application	
$application = new Application($array_uri);
$application->loadController($array_uri['controller']);
ob_end_flush();

?>